#include "dbengine.h"

DBEngine::DBEngine()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("filefridge.db");
    if(!db.isOpen()) {
        db.open();
    }

    createTables();
}

DBEngine::~DBEngine()
{
    db.close();
}

void DBEngine::createTables()
{
    /* Task table */
    db.exec("CREATE TABLE IF NOT EXISTS tasks ("
            "id INTEGER PRIMARY KEY ASC,"
            "task BLOB"
            ")"
            );

    /* Instances table */
    db.exec("CREATE TABLE IF NOT EXISTS instances ("
            "id INTEGER PRIMARY KEY ASC,"
            "task_id INTEGER,"
            "timestamp INTEGER,"
            "size INTEGER,"
            "instance_parent INTEGER"
            ")"
            );
}

QList<quint32> DBEngine::loadTaskIDs()
{
    QList<quint32> list;

    QSqlQuery query;
    query.exec("SELECT id FROM tasks");
    while(query.next()) {
        list.append(query.value(0).toUInt());
    }

    return list;
}
